package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import org.tuckey.web.filters.urlrewrite.utils.Log;

import com.corpcredit.Constants;
import com.corpcredit.model.Dwtz;
import com.corpcredit.model.Jbqk;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class DwtzForm extends BasePage implements Serializable {
    private GenericManager<Dwtz, Long> dwtzManager;
    private Dwtz dwtz = new Dwtz();
    private Long id;
    private Jbqk jbqk;

    public void setDwtzManager(GenericManager<Dwtz, Long> dwtzManager) {
        this.dwtzManager = dwtzManager;
    }

    public Dwtz getDwtz() {
        return dwtz;
    }

    public void setDwtz(Dwtz dwtz) {
        this.dwtz = dwtz;
    }

    public void setJbqk(Jbqk jbqk)
    {
         this.jbqk=jbqk;
    }
    
    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        dwtzManager.remove(dwtz.getId());
        addMessage("dwtz.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
    	
//    	Log.getLog(DwtzForm.class).debug("eidt********dwtz id: "+dwtz.getId()+"dwtz's jbqk id:"+dwtz.getJbqk().getId());
        if (id != null && id != 0) 
        {
            dwtz = dwtzManager.get(id);
        } else {
            dwtz = new Dwtz();
        }

        return "edit";
    }

    public String save() 
    {
        boolean isNew = (dwtz.getId() == null || dwtz.getId() == 0);
        
        dwtz.setJbqk(jbqk);
        dwtz.setUsername(getRequest().getRemoteUser());
        dwtz.setOptdate(new Date());
        dwtz.setOperation(isNew?Constants.OPERATION_ADD:Constants.OPERATION_UPDATE);
        
        dwtzManager.save(dwtz);

        String key = (isNew) ? "dwtz.added" : "dwtz.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else 
        {
        	this.id=this.dwtz.getId();
            return edit();
        }
    }
} 